package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.ShoppingGoodsService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ItemToShoppingGoodsDtoConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Stock;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.StockMapper;
import com.mall.shopping.dto.GoodsRequest;
import com.mall.shopping.dto.GoodsResponse;
import com.mall.shopping.dto.ShoppingGoodsVo;
import com.mall.shopping.model.ShoppingGoodsBo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @description: TODO
 * @author: jiaolei
 * @date: 2021/12/12 21:59
 */
@Service
public class ShoppingGoodsServiceImpl implements ShoppingGoodsService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemToShoppingGoodsDtoConverter itemToShoppingGoodsDtoConverter;
    @Autowired
    StockMapper stockMapper;

    @Override
    public GoodsResponse goods(GoodsRequest goodsRequest) {
        //sort是否排序需要看前端判断使用
        GoodsResponse goodsResponse = new GoodsResponse();

        try {
            //检查参数合法性，不合法报异常
            goodsRequest.requestCheck();
            ShoppingGoodsBo shoppingGoodsBo = goodsRequest.getShoppingGoodsBo();
            //根据cid查询item
            Example example = new Example(Item.class);
            Example.Criteria criteria = example.createCriteria().andEqualTo("cid", shoppingGoodsBo.getCid());
            if(shoppingGoodsBo.getPriceLte()!=null){
                criteria.andLessThanOrEqualTo("price", shoppingGoodsBo.getPriceLte());

            }
            if(shoppingGoodsBo.getPriceGt()!=null){
                criteria.andGreaterThanOrEqualTo("price", shoppingGoodsBo.getPriceGt());

            }
            String sort = shoppingGoodsBo.getSort();
            if(sort!=null){
                if(sort.equals("1")){
                    example.setOrderByClause("price asc");
                }else if(sort.equals("-1")){
                    example.setOrderByClause("price desc");
                }
            }
            PageHelper.startPage(shoppingGoodsBo.getPage(), shoppingGoodsBo.getSize());
            List<Item> items = itemMapper.selectByExample(example);
            PageInfo pageInfo = new PageInfo(items);
            long total = pageInfo.getTotal();

            //根据item的id查询stockCount
            for (Item item : items) {
                Stock stock = stockMapper.selectByPrimaryKey(item.getId());
                if (stock == null) {
                    item.setNum(0);
                } else {
                    item.setNum(stock.getStockCount());
                }
            }
            //将item转换成vo
            List<ShoppingGoodsVo.ShoppingGoodsDto> shoppingGoodsDtos = itemToShoppingGoodsDtoConverter.itemToShoppingGoodsDto(items);

            //将数据放入ShoppingGoodsVo
            ShoppingGoodsVo shoppingGoodsVo = new ShoppingGoodsVo();
            shoppingGoodsVo.setData(shoppingGoodsDtos);
            shoppingGoodsVo.setTotal((int) total);
            goodsResponse.setShoppingGoodsVo(shoppingGoodsVo);
            goodsResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            goodsResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            goodsResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            goodsResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }

        return goodsResponse;
    }
}
